package com.wc.算法提高课.A第一章_动态规划.背包模型.金明的预算方案;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/10 13:10
 * @description https://www.acwing.com/problem/content/description/489/
 */
public class Main {
    /**
     * 分组背包
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 70, M = 32010;
    static int[] f = new int[M];
    static int[][][] servants = new int[N][3][2];
    static int[][] masters = new int[N][2];
    static int n, m;

    public static void main(String[] args) {
        m = sc.nextInt();
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int w = sc.nextInt(), v = sc.nextInt(), q = sc.nextInt();
            v *= w;
            if (q == 0) {
                masters[i][0] = w;
                masters[i][1] = v;
            } else {
                int len = ++servants[q][0][0];
                servants[q][len][0] = w;
                servants[q][len][1] = v;
            }
        }
        for (int i = 1; i <= n; i++) {
            if (masters[i][0] > 0) {
                int size = servants[i][0][0];
                for (int j = m; j >= 0; j--) {
                    for (int u = 0; u < 1 << size; u++) {
                        int w = masters[i][0], v = masters[i][1];
                        for (int k = 0; k < size; k++) {
                            if ((u >> k & 1) == 1) {
                                w += servants[i][k + 1][0];
                                v += servants[i][k + 1][1];
                            }
                        }
                        if (j >= w) f[j] = Math.max(f[j], f[j - w] + v);
                    }
                }
            }
        }
        out.println(f[m]);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
